/*
 * @brief:
 * @Author: TzzzT
 * @Date: 2022-09-01 22:34:43
 * @LastEditTime: 2023-05-04 23:40:16
 * @LastEditors: TzzzT
 */

#pragma once

#include "stdint.h"
#include "RCS_protocol.h"
#include "player.h"
/////////////////SYSTEM/////////////////
#define SCREEN 1
#define NFC 1

typedef enum ble_state_e
{
    DISCONNECT_BLE,
    INITING_BLE,
    CONNECTED_BLE,
} ble_state_t;

// 事件
typedef enum rcs_event_e
{
    NO_EVENT,
    CONNECT_SUCCESS,
    REMOTE_COMM_EVENT,
    BE_HITTED,
    HIT_SUCCESS,
} rcs_event_t;

#define CONNECT_SUCCESS_EVENT_BIT 1 << CONNECT_SUCCESS
#define REMOTE_COMM_EVENT_BIT 1 << REMOTE_COMM_EVENT
#define BE_HITTED_EVENT_BIT 1 << BE_HITTED
#define HIT_SUCCESS_EVENT_BIT 1 << HIT_SUCCESS

#define BLUETOOTH_DEVICE_ADDR_LEN 6

typedef uint8_t bd_addr_t[BLUETOOTH_DEVICE_ADDR_LEN];

typedef struct device_s
{
    dev_class_t device_class;
    bd_addr_t cli_bda;
    uint8_t battery;
    bool low_batt;
    ble_state_t is_connect_master;
    bool wait_new_master;
    bool is_hold_scr;
    bool is_hold_nfc;
    bool is_net_up;
} device_t;
#define LOW_BATTERY_THRESHOLD 30

typedef struct game_s
{
    bool start;
    // uint32_t record_id;   //游戏记录id,每一局游戏这个id不一样
    player_t player;
} game_t;

typedef struct rcs_slave_system_s
{
    device_t device;
    game_t game;
    rcs_event_t event;
} rcs_slave_system_t;

void game_event_init(void);
void set_game_event(rcs_event_t new_event);
void get_game_event_state(rcs_event_t *new_event);

bool change_player_kill(player_kill_t kill);
void change_player_living(bool new_living, uint8_t *change);
void change_game_start(bool new_state, uint8_t *change);

/////////////////GAME/////////////////
uint8_t *make_verify_package(uint8_t *pack_len);
uint8_t *make_battery_package(uint8_t *pack_len);
uint8_t *make_disconnect_package(uint8_t *pack_len);
uint8_t *make_command_package(uint8_t command, uint8_t *pack_len);
uint8_t *make_ble_fire_success_package(uint8_t *pack_len);

bool change_player_bullet_nums(bullet_num_t new_bullet_num);
bool change_game_state(bool new_state);
bool change_living(bool new_state);
void analyse_master_package(uint8_t *buf, uint8_t buf_len);
/////////////////DEBUG/////////////////
uint8_t *event_name(rcs_event_t event);

//////////////////shock//////////////////

#define SHOCK_OUTPUT_PIN 5

void shock_init(void);
void set_shock(bool state);

//////////////////////////POWER//////////////////////////
#define POWER_PIN 10
void power_init(void);
void power_off(void);

